package com.youmu.shop.review.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.youmu.shop.admin.dao.AdminDao;
import com.youmu.shop.order.bean.OrderBean;
import com.youmu.shop.order.dao.OrderDao;
import com.youmu.shop.review.dao.PostReviewDao;
import com.youmu.shop.review.service.ReviewService;
import com.youmu.shop.system.bean.Admin;
import com.youmu.shop.system.bean.PosterReview;
import com.youmu.shop.user.dao.UserDao;

@Service
@Transactional
public class ReviewServiceImpl implements ReviewService{

	@Autowired
	private PostReviewDao postReviewDao;
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private AdminDao adminDao;
	
	@Override
	public Integer insertReview(PosterReview posterReview) {
		posterReview.setAdmin(orderDao.getById(posterReview.getPosterOrder()).getPosterId());
		posterReview.setCreateDate(new Date());
		posterReview.setModifyDate(new Date());
		int insertCode = postReviewDao.insertSelective(posterReview);
		if(insertCode > 0){
			OrderBean orderBean = orderDao.getById(posterReview.getPosterOrder());
			orderBean.setOrderStatus(6);
			orderDao.update(orderBean);
		}
		return insertCode;
	}

	@Override
	public Map<String, Object> selectPosterInfo(Long orderId) {
		OrderBean orderBean = orderDao.getById(orderId);
		Admin admin = adminDao.selectByPrimaryKey(orderBean.getPosterId());
		List<PosterReview> list = postReviewDao.selectPostReviewByPosterId(admin.getId());
		Integer allScore = 0;
		for (PosterReview posterReview : list) {
			if(posterReview.getPostScore() != null){
				allScore = allScore + posterReview.getPostScore();
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", admin.getName());
		map.put("image", admin.getWeixinPic());
		map.put("totalOrder", list.size());
		if(list.size() == 0){
			map.put("score", "4.0");
		}else{
			map.put("score", allScore / list.size() + ".0");
		}
		return map;
	}
	
	
	
}
